/**
 * <strong>Title : WorkUnitController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.platform.web.controller<br>
 * </strong> <strong>Description : </strong>@工作单元控制器@<br>
 * <strong>Create on : 2015-9-28 上午09:57:19<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.cm.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.Calendar;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.model.WorkUnitType;
import cn.b.sky.mes.cm.model.condition.WorkUnitCondition;
import cn.b.sky.mes.cm.model.condition.WorkUnitTypeCondition;
import cn.b.sky.mes.cm.service.CalendarService;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.mes.cm.service.WorkUnitService;
import cn.b.sky.mes.cm.service.WorkUnitTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : WorkUnitController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.platform.web.controller<br>
 * </strong> <strong>Description : </strong>作业单元控制器<br>
 * <strong>Create on : 2015-9-28 上午09:57:19<br>
 * </strong>
 * <p>
 * 
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */

@Controller
@RequestMapping(value = "/workUnit/*")
public class WorkUnitController extends SecurityController {
  @Resource
  private WorkUnitService workUnitService;

  @Resource
  private WorkUnitTypeService workUnitTypeService;

  @Resource
  WorkCenterService workCenterService;
  
  @Resource
  private CalendarService calendarService;

  @RequestMapping(value = "workUnitList")
  public String workUnitList(Model model, @ModelAttribute("condition") WorkUnitCondition workUnitCondition,
      String navTabId) {

    workUnitCondition.setSiteCode(getSiteCode());
    workUnitCondition.setOrderField("CreateDateTime");
    List<WorkUnit> workUnits = workUnitService.selectWorkUnitList(workUnitCondition);
    int count = workUnitService.getWorkUnitCount(workUnitCondition);
    workUnitCondition.setData(workUnits);
    workUnitCondition.setTotalCount(count);
    model.addAttribute("page", workUnitCondition);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/workUnit/workUnit_list";
  }

  @RequestMapping(value = "selectWorkUnitType")
  public String selectWorkUnitType(String navTabId, String lookupType,
      @ModelAttribute("condition") WorkUnitTypeCondition condition, Model model) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    condition.setOrderField("CreateDateTime");
    List<WorkUnitType> workUnitTypes = workUnitTypeService.selectWorkUnitTypeList(condition);
    int count = workUnitTypeService.getWorkUnitTypeCount(condition);
    model.addAttribute("pattern", "add");
    condition.setData(workUnitTypes);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    if (!StringUtils.isBlank(lookupType) && "radio".equals(lookupType)) {
      return "cm/workUnit/workUnitType_select_radio"; // 单选查找带回
    }
    return "cm/workUnit/workUnitType_select";
  }

  @RequestMapping("workUnitTypeSuggest")
  @ResponseBody
  public Object getWorkUnitTypeSuggest(@ModelAttribute("condition") WorkUnitTypeCondition condition) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLockFlag(0);
    return workUnitTypeService.selectWorkUnitTypeSuggests(condition);
  }

  @RequestMapping(value = "addWorkUnit")
  public String addWorkUnit(String navTabId, Model model) {
    WorkUnitTypeCondition condition = new WorkUnitTypeCondition();
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");

    List<WorkCenter> workCenters = workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), 0);
    model.addAttribute("workCenters", workCenters);
    //工作日历
    List<Calendar> calendars = calendarService.selectAllUnlockCalendars(this.getSiteCode());
    model.addAttribute("calendars", calendars);
    return "cm/workUnit/workUnit_add";
  }

  @RequestMapping(value = "modifyWorkUnit")
  public String modifyWorkUnit(String navTabId, String id, Model model) {
    WorkUnit workUnit = workUnitService.getWorkUnit(id, getSiteCode());
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("workUnit", workUnit);
    List<WorkUnitType> workUnitTypes = workUnitTypeService.selectAssignByUnit(id, getSiteCode());
    List<WorkCenter> workCenters = workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), 0);
    model.addAttribute("workCenters", workCenters);
    model.addAttribute("workUnitTypes", workUnitTypes);
    model.addAttribute("pattern", "modify");
    //工作日历
    List<Calendar> calendars = calendarService.selectAllUnlockCalendars(this.getSiteCode());
    model.addAttribute("calendars", calendars);
    return "cm/workUnit/workUnit_add";
  }

  @RequestMapping(value = "viewWorkUnit")
  public String viewWorkUnit(String navTabId, String id, Model model) {

    WorkUnit workUnit = workUnitService.getWorkUnit(id, getSiteCode());

    List<WorkUnitType> workUnitTypes = workUnitTypeService.selectAssignByUnit(id, getSiteCode());
    List<WorkCenter> workCenters = workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), 0);

    model.addAttribute("workCenters", workCenters);
    model.addAttribute("workUnitTypes", workUnitTypes);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("workUnit", workUnit);
    model.addAttribute("pattern", "read");
    return "cm/workUnit/workUnit_add";

  }

  /**
   * 
   * <strong>Title : validateWorkUnitCode<br></strong>
   * <strong>Description : </strong>验证工作单元是否存在<br> 
   * <strong>Create on : 2016-8-12 下午3:39:50<br></strong>
   * <p>
   * @param workUnitCode
   * @param model
   * @return
   * boolean
   * @throws 
   * @author JinQuan.Fu<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "validateWorkUnitCode")
  @ResponseBody
  public boolean validateWorkUnitCode(String workUnitCode, Model model) {
    if (!StringUtils.isBlank(workUnitCode)) {
      WorkUnitCondition workUnitCondition = new WorkUnitCondition();
      workUnitCondition.setWorkUnitCode(workUnitCode);
      workUnitCondition.setSiteCode(SecurityContextUtil.getSiteCode());
      int count = workUnitService.getWorkUnitExactCount(workUnitCondition);
      return count == 0;
    }
    return true;
  }
  
  @RequestMapping(value = "chckWorkUnitCode")
  @ResponseBody
  public boolean chckWorkUnitCode(String workUnitCode, Model model) {
    if (!StringUtils.isBlank(workUnitCode)) {
      WorkUnitCondition workUnitCondition = new WorkUnitCondition();
      workUnitCondition.setWorkUnitCode(workUnitCode);
      workUnitCondition.setSiteCode(SecurityContextUtil.getSiteCode());
      int count = workUnitService.getWorkUnitExactCount(workUnitCondition);
      return count != 0;
    }
    return false;
  }

  @RequestMapping(value = "saveWorkUnit")
  @ResponseBody
  public DWZResult saveWorkUnit(Model model, String navTabId, String id, String ids, WorkUnit workUnit,
      HttpServletRequest request) {
    if (workUnit == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }

    setCreateInfo(workUnit);
    workUnit.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isBlank(id)) {
      workUnitService.insertWorkUnit(workUnit);

      if (StringUtils.isNotBlank(ids)) {
        String[] workUnitTypeIds = ids.split(",");
        if (workUnitTypeIds.length > 0) {
          workUnitService.insertWorkUnitTypeAssign(workUnit, workUnitTypeIds, getSiteCode());
        }
      }

      String forwardUrl = getForwardUrl(request) + "/workUnit/workUnitList?navTabId=" + navTabId;
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, forwardUrl, null);
    } else {
      workUnitService.updateWorkUnit(workUnit);
      if (StringUtils.isNotBlank(ids)) {
        String[] workUnitTypeIds = ids.split(",");
        if (workUnitTypeIds.length > 0) {
          workUnitService.updateWorkUnitTypeAssign(workUnit, workUnitTypeIds, getSiteCode());
        }
      } else {
        workUnitTypeService.deleterAssignByUnit(workUnit, getSiteCode());
      }
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
    }

  }

  @RequestMapping("deleteWorkUnit")
  @ResponseBody
  public DWZResult deleteWorkUnit(Model model, String navTabId, String id, HttpServletRequest request) {

    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] workUnits = ids.split(",");
    int len = workUnits.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = workUnitService.deleteWorkUnits(workUnits[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }

  }

  @RequestMapping("workUnitLookUp")
  public String workUnitLookUp(Model model, String lookupType, @ModelAttribute("condition") WorkUnitCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setOrderField("CreateDateTime");
    condition.setLockFlag(0);
    List<WorkUnit> workUnits = workUnitService.selectWorkUnitList(condition);
    int count = workUnitService.getWorkUnitCount(condition);
    condition.setData(workUnits);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    if (!StringUtils.isBlank(lookupType) && "radio".equals(lookupType)) {
      return "cm/workUnit/workUnit_lookup_radio";
    }
    return "cm/workUnit/workUnit_lookup";
  }

  @RequestMapping("workUnitSuggest")
  @ResponseBody
  public Object componentSuggest(@ModelAttribute("condition") WorkUnitCondition condition) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLockFlag(0);
    return workUnitService.selectWorkUnitSuggest(condition);
  }

}
